package com.formulas.component.dto;

import java.io.Serial;

/**
 * 返回一条记录的响应
 * @author yiwannuofulasi
 * @date 2023/6/2 10:37
 */
public class SingleResponse<T> extends Response{

    @Serial
    private static final long serialVersionUID = 1L;

    /**数据对象Object**/
    private T data;

    /**
     * 获取数据对象Object
     * @return Object
     */
    public T getData() {
        return data;
    }

    /**
     * 设置数据对象Object
     */
    public void setData(T data) {
        this.data = data;
    }

    /**
     * 成功构建响应
     * @return SingleResponse
     */
    public static SingleResponse buildSuccess() {
        SingleResponse response = new SingleResponse();
        response.setSuccess(true);
        return response;
    }

    /**
     * 失败构建响应
     * @param errCode 错误代码
     * @param errMessage 错误消息
     * @return SingleResponse
     */
    public static SingleResponse buildFailure(String errCode, String errMessage) {
        SingleResponse response = new SingleResponse();
        response.setSuccess(false);
        response.setErrCode(errCode);
        response.setErrMessage(errMessage);
        return response;
    }

    /**
     * 指定对象构建单记录回复
     * @param data Object
     * @return SingleResponse<T>
     * @param <T> Object
     */
    public static <T> SingleResponse<T> of(T data) {
        SingleResponse<T> response = new SingleResponse<>();
        response.setSuccess(true);
        response.setData(data);
        return response;
    }
}
